
#include <BPainter>
#include <BScrollBar>
#include <BMouseEvent>
#include <BSizePolicy>
#include <BChangeEvent>
#include <BVBoxLayout>
#include <BHBoxLayout>

#include <BScrollWidget>
#include "member_BScrollWidget.h"

using namespace BWE;

#define member					(*(member_BScrollWidget*)_ptr)
#define member_allocate()		_ptr = new member_BScrollWidget(this)
#define member_release()		delete (member_BScrollWidget*)_ptr

BScrollWidget::BScrollWidget(BWidget* parent) : BScrollArea(parent)
{
	member_allocate();
	this->setOrientation(Orientation_Vertical);
}
BScrollWidget::BScrollWidget(Orientation orientation, BWidget* parent) : BScrollArea(parent)
{
	member_allocate();
	this->setOrientation(orientation);
}
BScrollWidget::~BScrollWidget()
{
	member_release();
}

void BScrollWidget::setOrientation(Orientation orientation)
{
	if (member.orientation != orientation)
	{
		member.orientation = orientation;
		BLayout* layout = 0;
		if (member.orientation == Orientation_Vertical)
		{
			BVBoxLayout* vlayout = new BVBoxLayout();
			for (int i = 0; i < member.widgets.size(); i++)
				vlayout->addWidget(member.widgets[i]);
			layout = vlayout;
		}
		if (member.orientation == Orientation_Horizontal)
		{
			BHBoxLayout* hlayout = new BHBoxLayout();
			for (int i = 0; i < member.widgets.size(); i++)
				hlayout->addWidget(member.widgets[i]);
			layout = hlayout;
		}
		member.layout = layout;
	}
}
Orientation BScrollWidget::orientation() const
{
	return member.orientation;
}

bool BScrollWidget::addWidget(BWidget* widget)
{
	if (!widget)
		return false;
	if(!member.widgets.contain(widget))
	{
		member.widgets.append(widget);
		this->addChild(widget);
		if (member.orientation == Orientation_Vertical)
		{
			BVBoxLayout* vlayout = (BVBoxLayout*)member.layout.get();
			vlayout->addWidget(widget);
		}
		if (member.orientation == Orientation_Horizontal)
		{
			BHBoxLayout* hlayout = (BHBoxLayout*)member.layout.get();
			hlayout->addWidget(widget);
		}
		widget->setFloating(false);
		member.dirtyArea = true;
		this->fresh();
	}
	return true;
}
bool BScrollWidget::removeWidget(BWidget* widget)
{
	if (!widget)
		return false;
	if (member.widgets.contain(widget))
	{
		disconnect(widget);
		member.widgets.remove(widget);
		this->removeChild(widget);
		member.layout->remove(widget);
		this->fresh();
		return true;
	}
	return false;
}
void BScrollWidget::clearWidgets()
{
	if (member.widgets.size())
	{
		for (int i = 0; i < member.widgets.size(); i++)
		{
			this->removeChild(member.widgets[i]);
		}
		member.widgets.clear();
		member.layout->clear();
		this->fresh();
	}
}

BWidget* BScrollWidget::widget(int index) const
{
	return member.widgets(index);
}

void BScrollWidget::scrollArea()
{
	if(member.layout)
	{
		BRect area = this->area();
		member.layout->setRect(area);
		member.layout->adjust();
		this->fresh();
	}
}

void BScrollWidget::changeEvent(const BChangeEvent& event)
{
	this->fresh();
}
void BScrollWidget::resizeEvent(const BEvent& event)
{
	BScrollArea::resizeEvent(event);
	member.dirtyArea = true;
}

void BScrollWidget::freshEvent(const BEvent& event)
{
	BScrollArea::freshEvent(event);
	if (member.dirtyArea)
	{
		member.updateArea();
		member.dirtyArea = false;
	}
}
void BScrollWidget::paintEvent(const BEvent& event)
{
	BPainter painter(this);

	painter.drawBorder();
	painter.drawPerch();
	painter.drawMargin();
}

